<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title></title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="../../static/lib/layui/css/layui.css" media="all" />
  <link rel="stylesheet" href="../../static/css/admin.css" media="all" />
</head>

<body class="tmm-modal-body">
  <div class="layui-container">
    <form class="layui-form" action="">
      <div class="">新增产品：</div>
      <table class="layui-table" id="product_table">
        <colgroup>
          <col width="150">
          <col width="150">
          <col width="200">
          <col>
        </colgroup>
        <thead>
          <tr>
            <th>产品名称</th>
            <th>实收尾款</th>
          </tr>
        </thead>
        <tbody id="addTbody">
        </tbody>
      </table>
      <div class="layui-row">
        <div class="layui-col-sm8">
          <div class="layui-form-item">
            <label class="layui-form-label" style="text-align: left;">关联用户</label>
            <div class="layui-input-block">
              <input type="text" name="phone" required lay-verify="required" placeholder="手机号" autocomplete="off"
                class="layui-input">
            </div>
          </div>
        </div>
        <div class="layui-col-sm4 layui-layer-btn" style="text-align: right;">
          <a class="tmm-btn" href="javascript:void(0)" id="add_product_btn">新增产品</a>
        </div>
      </div>
      <div class="layui-row">
        <div class="layui-col-md12 tmm-table-summery" id="sumMoney">
        </div>
      </div>
    </form>
  </div>
  <div style="display: none;">
  </div>
  <script src="../../static/lib/layui/layui.js"></script>
  <script>
    layui.config({
      base: "../../static/js/"
    }).extend({
      index: 'index'
    }).use(['index', 'form'], function () {
      const $ = layui.$;

      function addProduct(d){
        if (d) {
          for (let i = 0; i < d.length; i++) {
            const di = d[i];
            $('#product_table tbody').append(`<tr>
              <td>${di.name}</td>
              <td>
                <input type="text" name="addMoneyy" placeholder="请输入金额" class="layui-input">
              </td>
            </tr>`);
          }
        }
      }
      
      $('#add_product_btn').click(function() {
        var pPopIndex = parent.layer.open({
          type: 2,
          id: 'add-product-win',
          title: "新增产品",
          content: "${path}/order/addGoods",
          maxmin: !0,
          area: ["850px", "600px"],
          btn: ["确定新增", "取消"],
          yes: function(index, layero) {
              var body = parent.layer.getChildFrame('body', pPopIndex); //得到iframe页的body内容
              var addTable = body.find("#product_table tbody tr");

              var add = [];
              var flag = false;
              addTable.each(function () {
                  var trType = $(this).attr("data-type");
                  var goods = {};
                  if(trType==0){
                      goods.addId = $(this).find('.layui-select').val();
                      goods.addName = $(this).find('.layui-select').find('option:selected').text();
                      goods.categoryPid = $(this).find('.layui-select').find('option:selected').attr("categoryPid");
                      goods.customerName = '';
                      goods.addDoctorId = 0;
                      goods.price = $(this).find('.layui-select').find('option:selected').attr("price");
                  }else{
                      goods.addId = -1;
                      goods.addName = $(this).find('.layui-select').text()
                      goods.categoryPid = $(this).find('.categoryPid').val();
                      goods.customerName = $(this).find('.custom').val();
                      goods.addDoctorId = $(this).find('.doctor').val();
                      goods.price = 0;
                  }
                  if(trType==0){
                      if(goods.addName=='产品名称'){
                          flag = true;
                      }
                  }else{
                      if(goods.customerName == '' || goods.addDoctorId == '' || goods.categoryPid == ''){
                          flag = true;
                      }
                  }
                  add.push(goods);
              })
              if(flag){
                  parent.layer.alert("请录入完整产品信息");
                  return;
              }
              parent.layer.close(index);

              for(var i=0;i<add.length;i++){
                  var name = add[i].addName;
                  $("#addTbody").append('<tr>\n' +
                      '            <td>'+(name!=null&&name!=''?name:add[i].customerName)+'</td>\n' +
                      '            <td>\n' +
                      '              <input type="text" name="money" id="addMoney" placeholder="请输入金额" value="'+add[i].price+'" class="layui-input">\n' +
                      '              <input type="hidden" name="goods_id" value="'+add[i].addId+'" id="addId" placeholder="" class="layui-input">\n' +
                      '              <input type="hidden" name="goods_name" value="'+(add[i].customerName!=null&&add[i].customerName!=''?add[i].customerName:add[i].addName)+'" id="addCustomName" placeholder="" class="layui-input">\n' +
                      '              <input type="hidden" name="categoryPid" value="'+add[i].categoryPid+'" id="categoryPid" placeholder="" class="layui-input">\n' +
                      '              <input type="hidden" name="doctor_id" value="'+add[i].addDoctorId+'" id="doctor_id" placeholder="" class="layui-input">\n' +
                      '            </td>\n' +
                      '          </tr>')
              }

              parent.layer.close(pPopIndex);
          }
        })
      });
      $(function() {
          //输入框的值改变时触发
          $("#addMoney").on("input", function (e) {
              $("#sumMoney").empty().append('合计实收：'+e.delegateTarget.value)
          });
      });
    })
  </script>
</body>

</html>